﻿#region usings
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Web.Models;
using System.Runtime.Serialization;
#endregion
namespace InsuranceAdjuster.OccasionallyConnected.Messages {
    [DataContractAttribute]
    public class SaveClaimMessage : Message {

        public SaveClaimMessage(Claim claim, Guid callerGuid) {
            ClaimGuid = claim.ClaimGuid;
            CallerGuid = callerGuid;

            CacheRules = MessageDataSource.ServerOnly;

            MessageName = String.Format("Save Claim {0}", claim.PoliceReportNo);
            IsDurable = true;
            AutoClearOnArchive = false;

            CurrentEntityManager.GetObjectStore().SetEntity(claim);
        }

        protected override void OnlineOperation() {
            MarkAsyncStarted();

            var MyClaim = CurrentEntityManager.GetObjectStore().GetEntity<Claim>(ClaimGuid);

            var MyService = GetDataService();
            MyService.DataContext.Claims.Attach(MyClaim);
            // Trigger a dirty write after Attach to tell RIA Services it's changed
            MyClaim.LastEdit = DateTime.UtcNow;
            MyService.SaveClaim(submitOp => {
                    CurrentEntityManager.GetObjectStore().SetEntity(MyClaim);
                    this.HasError = submitOp.HasError;
                    this.Error = submitOp.Error;
                    MarkAsyncFinished();
                }, null);
        }

        protected override List<MessageResult> CompileServerResults() {
            var Output = new List<MessageResult>();

            if (HasError == false) {
                var MyClaim = CurrentEntityManager.GetObjectStore().GetEntity<Claim>(ClaimGuid);
                Output.Add(new MessageResult(Verb.Update, Origin.Server, MyClaim));
            }

            return Output;
        }

        #region Initilalization
        public SaveClaimMessage() {
            // Called when loading a durable message
        }

        public SaveClaimMessage(Claim claim)
            : this(claim, Guid.Empty) {
        }

        public Guid ClaimGuid {
            get { return GetMessageData<Guid>("ClaimGuid"); }
            set { SetMessageData<Guid>("ClaimGuid", value); }
        }

        public Boolean HasError {
            get { return GetMessageData<Boolean>("SaveMessageHasError", false); }
            protected set { SetMessageData<Boolean>("SaveMessageHasError", value); }
        }
        public Exception Error {
            get { return GetMessageData<Exception>("SaveMessageError"); }
            protected set { SetMessageData<Exception>("SaveMessageError", value); }
        }

        protected override Boolean PreProcessAction() {
            return true;
        }

        #endregion
    }
}
